﻿using InterviewAppPhone.ViewModels;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Net.NetworkInformation;
using System.Windows;
using System.Windows.Navigation;

namespace InterviewAppPhone.Views
{
    public partial class SuggerisciDomanda : PhoneApplicationPage
    {
        public SuggerisciDomandaViewModel ViewModel;

        public SuggerisciDomanda()
        {
            InitializeComponent();
        }


        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (ViewModel == null)
            {
                ViewModel = new SuggerisciDomandaViewModel();
                ViewModel.VMesitoPropostaDomanda += ViewModel_VMesitoPropostaDomanda;
            }
            ViewModel.IsConnection = DeviceNetworkInformation.IsNetworkAvailable;
            DataContext = ViewModel;
            base.OnNavigatedTo(e);
        }

        private void ViewModel_VMesitoPropostaDomanda(object sender, System.EventArgs e)
        {
            if ((bool)sender)
            {
                StOK.Begin();
                StOK.Completed += StOK_Completed;
            }
            else
            {
            
                //TODO appena possibile fare StError
            }

        }

        void StOK_Completed(object sender, System.EventArgs e)
        {
            NavigationService.GoBack();
        }

        private void InviaDomanda_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            var esito = MessageBox.Show("Sei sicuro di voler inviare questa domanda?", "", MessageBoxButton.OKCancel);
            switch (esito)
            {
                case MessageBoxResult.Cancel:
                    break;

                case MessageBoxResult.OK:
                    ViewModel.InviaDomanda();
                    break;

                default:
                    break;
            }
        }


    }
}